/*
 * Created by SharpDevelop.
 * User: sburke
 * Date: 3/18/2008
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Timers;
namespace PSCEditor
{
	/// <summary>
	/// PSC Editor with Syntax Highlighting.
	/// </summary>
	public partial class Editor : Form
	{
		public static bool okToParse=false;
        public static string file;
		public Editor(string filename)
		{
			InitializeComponent();
            file = filename;
			StreamReader fin=new StreamReader(filename);
			string data=fin.ReadToEnd();
			Regex r=new Regex("\\n");
			string [] lines=r.Split(data);
			foreach(string l in lines) {
				parse(l);
			}
			okToParse=true;
		}

		void SaveToolStripMenuItemClick(object sender, EventArgs e)
		{
            try
            {
                TextWriter fout = new StreamWriter(file);
                Regex r = new Regex("\\n");
                string text = Source.Text.TrimEnd('\n');
                string[] temp = r.Split(text);
                foreach (string line in temp)
                {
                    fout.WriteLine(line);
                }
                //fout.Write(Source.Text);
                fout.Close();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
		}
		
		void CloseToolStripMenuItemClick(object sender, EventArgs e)
		{
			this.Close();
		}
		void parse(string line) {
			
            string[] keys = { "Load", "When", "Until", "If", "Par", "Seq", "Rest", "Defend", "Danger", "Loot", "Pull", "Harvest", "Hotspots", "Walk", "Vendor", "Train", "Mail", "BGQueue", "Taxi", "Talent", "Log", "String", "CastSpell", "Buff", "QuestPickup", "QuestGoal", "QuestHandin" };
            string [] symbols ={ "{","}","(",")"};
			string [] vars={"$cond","$MyLevel","$MyClass","$MyRace","$MyZone","$MySubZone","$MyDurability","$MyHealth","$MyMana","$MyEnergy","$MyX","$MyY","$MyZ","$IsStealthed","$ItemCount","$FreeBagSlots","$BGQueued","$AlreadyTrained"};
			
			int x;
			Regex r=new Regex("([ \\t{}():;])");
			string [] tokens=r.Split(line);
			Source.SelectionColor=Color.Black;
			Source.SelectionFont=new Font("Courier New", 10, FontStyle.Regular);
			foreach (string token in tokens) {
				Source.SelectionColor=Color.Black;
				Source.SelectionFont=new Font("Courier New", 10, FontStyle.Regular);
				for(x=0;x<keys.Length;x++) {
					if (keys[x]==token) {
						Source.SelectionColor=Color.Blue;
						Source.SelectionFont=new Font("Courier New", 10, FontStyle.Bold);
						break;
					}
				}
				for(x=0;x<symbols.Length;x++) {
					if(symbols[x]==token) {
						Source.SelectionColor=Color.Black;
						Source.SelectionFont=new Font("Courier New", 10, FontStyle.Bold);
						break;
					}
				}
				for(x=0;x<vars.Length;x++) {
					if(vars[x]==token) {
						Source.SelectionColor=Color.Green;
						Source.SelectionFont=new Font("Courier New", 10, FontStyle.Italic);
						break;
					}
				}
				Source.SelectedText=token;
			}
			
		}
		
		void SourceTextChanged(object sender, EventArgs e)
		{
			if(okToParse) {
				okToParse=false;
				/*
                string data=Source.Text;
				int cur=Source.SelectionStart;
				Source.Text="";;
				Regex r=new Regex("\\n");
				string [] lines=r.Split(data);
				foreach(string l in lines) {
					parse(l);
                    Source.SelectedText = System.Environment.NewLine;
				}
                Source.Text.TrimEnd('\n');
				Source.SelectionStart=cur;
				Source.ScrollToCaret();
                */

                int start = 0, end = 0;
                int cur;
                for (start = Source.SelectionStart - 1; start > 0; start--)
                {
                    if (Source.Text[start] == '\n')
                    {
                        start++;
                        break;
                    }
                }
                for (end = Source.SelectionStart; end < Source.Text.Length; end++)
                {
                    if (Source.Text[end] == '\n')
                        break;
                }
                cur = Source.SelectionStart;
                String l = Source.Text.Substring(start, end-start);
                Source.SelectionStart = start;
                Source.SelectionLength = end - start;
                Source.SelectedText="";
                parse(l);
                Source.SelectionStart = cur;
				okToParse=true;
			}
		}
        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 9)
            {
                Source.SelectedText = "    ";
            }
            
        }
    }
}
